package com.eastfair.imaster.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.imaster.dao.ConferenceSpeakerMapper;
import com.eastfair.imaster.dto.ConferenceSpeakerDTO;
import com.eastfair.imaster.entity.ConferenceSpeaker;
import com.eastfair.imaster.mydao.MyConferenceSpeakerMapper;
import com.eastfair.imaster.service.ConferenceSpeakerService;
import com.eastfair.imaster.vo.ConferenceSpeakerVO;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 业务实现类
 * 论坛嘉宾
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class ConferenceSpeakerServiceImpl extends SuperServiceImpl<ConferenceSpeakerMapper, ConferenceSpeaker> implements ConferenceSpeakerService {

    @Autowired
    ProjectCoreServiceFeign projectCoreServiceFeign;
    @Autowired
    private MyConferenceSpeakerMapper myConferenceSpeakerMapper;


    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(ConferenceSpeaker entity) {
        UpdateWrapper<ConferenceSpeaker> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(ConferenceSpeaker::getProjectId, entity.getProjectId()).eq(ConferenceSpeaker::getId, entity.getId());
        entity.setProjectId(null);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(ConferenceSpeaker model) {
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(ContextUtil.getProjectId()));
        model.setProjectId(ContextUtil.getProjectId());
        return R.successDef();
    }

    @Override
    public R<Boolean> saveConferenceSpeakers(List<ConferenceSpeakerDTO> conferenceSpeakerDTOList, Long conferenceId) {
        if (conferenceSpeakerDTOList == null || conferenceSpeakerDTOList.isEmpty()) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }

        // 批量生成雪花id
        List<Long> ids = batGetSelfUid(conferenceSpeakerDTOList.size());

        // 论坛嘉宾入库
        List<ConferenceSpeaker> conferenceSpeakers = ConvertUtil.convertList(conferenceSpeakerDTOList, ConferenceSpeaker.class);
        for (int i = 0; i < conferenceSpeakers.size(); i++) {
            conferenceSpeakers.get(i).setProjectId(ContextUtil.getProjectId()).setConferenceId(conferenceId)
                    .setId(ids.get(i));
        }
        return R.success(saveBatch(conferenceSpeakers));
    }

    @Override
    public R<Boolean> deleteByConferenceId(Long conferenceId) {
        if (conferenceId == null) {
            return R.fail(ExceptionCode.ERROR_NULL_ID);
        }
        UpdateWrapper<ConferenceSpeaker> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ConferenceSpeaker::getProjectId, ContextUtil.getProjectId())
                .eq(ConferenceSpeaker::getConferenceId, conferenceId).set(ConferenceSpeaker::getIsDeleted, BusinessConstant.DELETE_YES);
        return R.success(update(updateWrapper));
    }

    @Override
    public R<Boolean> deleteAndSaveConferenceSpeakers(List<ConferenceSpeakerDTO> conferenceSpeakerDTOList, Long conferenceId) {
        deleteByConferenceId(conferenceId);
        saveConferenceSpeakers(conferenceSpeakerDTOList, conferenceId);
        return R.success(Boolean.TRUE);
    }

    @Override
    public R<List<ConferenceSpeakerVO>> getByConferenceId(Long conferenceId, String sortOrderType) {
        if (conferenceId == null) {
            return R.fail(ExceptionCode.ERROR_NULL_ID);
        }

        return R.success(myConferenceSpeakerMapper.getByConferenceId(conferenceId, sortOrderType));
    }

    /**
     * 批量生成雪花id
     *
     * @param size
     * @return
     */
    private List<Long> batGetSelfUid(Integer size) {
        List<Long> ids = DefaultUidGenerator.getBatUidOfProjectId(ContextUtil.getProjectId(), size);
        if (ids == null || ids.isEmpty()) {
            throw new RuntimeException("System exception, generate snowflake id error");
        }
        return ids;
    }
}
